package com.wln.sms.sms.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.wln.common.utils.R;
import com.wln.sms.sms.entity.OrderInfoEntity;
import com.wln.sms.sms.feign.GoodsFeignService;
import com.wln.sms.sms.feign.OrderFeignService;
import com.wln.sms.sms.service.OrderInfoService;
import com.wln.sms.sms.util.IdWorker;
import com.wln.sms.sms.vo.ActivityGoodsVo;
import com.wln.sms.sms.vo.AddOrderVo;
import com.wln.sms.sms.vo.GoodsVo;
import com.wln.sms.sms.vo.MiaoActivityEntity;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wln.common.utils.PageUtils;
import com.wln.common.utils.Query;

import com.wln.sms.sms.dao.GoodsActivityDao;
import com.wln.sms.sms.entity.GoodsActivityEntity;
import com.wln.sms.sms.service.GoodsActivityService;


@Service("goodsActivityService")
public class GoodsActivityServiceImpl extends ServiceImpl<GoodsActivityDao, GoodsActivityEntity> implements GoodsActivityService {
    @SuppressWarnings("all")
    @Autowired
    private GoodsActivityDao goodsActivityDao;

    @Autowired
    private RedisTemplate redisTemplate;
    public static int i = 0;

    @Autowired
    private OrderInfoService orderInfoService;
    @SuppressWarnings("all")
    @Autowired
    private OrderFeignService orderFeignService;

    @Autowired
    private GoodsFeignService goodsFeignService;

    @Autowired
    private IdWorker idWorker;

    @SuppressWarnings("all")
    @Autowired
    private RabbitTemplate rabbitTemplate;


    @SuppressWarnings("all")
    @Autowired
    private RedissonClient client;

//    @Override
//    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<GoodsActivityEntity> page = this.page(
//                new Query<GoodsActivityEntity>().getPage(params),
//                new QueryWrapper<GoodsActivityEntity>()
//        );
//
//        return new PageUtils(page);
//    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String page1 = (String) params.get("page");
        RLock lock = client.getLock("lock");
        try {
            boolean b = lock.tryLock(30, TimeUnit.SECONDS);
            if (b) {
                BoundHashOperations activity = redisTemplate.boundHashOps("activity");
                IPage<GoodsActivityEntity> list = (IPage<GoodsActivityEntity>) activity.get(page1);
                if (list != null) {
                    System.out.println("命中缓存");
                    return new PageUtils(list);
                } else {
                    IPage<GoodsActivityEntity> page = this.page(
                            new Query<GoodsActivityEntity>().getPage(params),
                            new QueryWrapper<GoodsActivityEntity>()
                    );
                    activity.put(page1, page);
                    activity.expire(30, TimeUnit.SECONDS);
                    i++;
                    System.out.println("缓存未命中***************************" + i);
                    return new PageUtils(page);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return null;
    }


    @Override
    public ActivityGoodsVo showInfo(Long activityId) {
        ActivityGoodsVo activityGoodsVo = goodsActivityDao.findById(activityId);
        return activityGoodsVo;
    }

    @Override
    public GoodsActivityEntity getByGoodsId(Long goodsId) {
        GoodsActivityEntity goods_id = goodsActivityDao.selectOne(new QueryWrapper<GoodsActivityEntity>().eq("goods_id", goodsId));
        return goods_id;
    }

}